<?php

/**
 * @copyright       : FOSP
 * @version         : 20120322
 * @name            : Fpt_Business_League_Adapter_Mysql
 * @todo            : MySQL access table league
 * @author          : QuangTM
 */
class Fpt_Business_Season_Adapter_Mysql extends Fpt_Business_Season_Adapter_Abstract
{

    /**
     * Get Fpt_Business_Season_Adapter_Mysql's instance
     * @return Fpt_Business_Season_Adapter_Mysql 
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get list season by ids
     * @param array $arrSeasonIDs
     * @return array|bool
     * @author QuangTM
     */
    public function getListSeasonByIDs($arrSeasonIDs)
    {
        try
        {
            // Convert from array to string
            $strSeasonIds = implode(',', $arrSeasonIDs);

            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_fesport_getListSeasonByIDs(:p_seasonids);');

            // Bind param
            $stmt->bindParam('p_seasonids', $strSeasonIds, PDO::PARAM_STR);

            // Execute
            $stmt->execute();

            // Get result
            $result = array();

            // Get format data
            $formatInstance = new Fpt_Business_Season_Metadata_Season();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $formatInstance->init($row)->getFormatedData());

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt, $strSeasonIds, $formatInstance);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get list season by league
     * @param int $leagueID
     * @return array|bool
     * @author QuangTM
     */
    public function getListSeasonByLeagueID($leagueID)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_fesport_getListSeasonByLeague(:p_leagueid);');

            // Bind param
            $stmt->bindParam('p_leagueid', $leagueID, PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Get result
            $result = array();

            // Get format data
            $formatInstance = new Fpt_Business_Season_Metadata_Season();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $formatInstance->init($row)->getFormatedData());

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt, $strSeasonIds, $formatInstance);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}